package com.ruipengkj.view.memberView;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TSerShiftType;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.service.TSerShiftTypeService;

public class SerShiftTypeAdd extends IDialog {
	private static final long serialVersionUID = 1L;
	private JButton okButton;//确定
	private JButton cancel;//取消
	private JLabelJTextFieldComponent number = null;//轮牌编号
	private JLabelJTextFieldComponent Name = null;//轮牌名称
	public boolean isSuccess=false;//判断是否添加成功
	private TSerShiftTypeService tSerShiftTypeService=BuildSpringApplication.getInstance().getTSerShiftTypeServiceImpl();
	
	public SerShiftTypeAdd(JDialog jd, String title, boolean b) {
		
		super(jd,title,b);
		this.init();
		this.setSize(300, 550);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
	}
	private void init(){
		this.initViews();
		this.initListener();
		//this.getElements();
	}
	private void initListener(){
		okButton.addActionListener(this);
		cancel.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		super.actionPerformed(e);
		Date date=new Date();
		TSerShiftType tSerShiftType = new TSerShiftType();
		
			tSerShiftType.setTypeID(number.getText().getText().trim());
			if(e.getSource()==okButton){
				//确定
			  if(Name.getText().equals("")){
					JOptionPane.showMessageDialog(null, "轮牌名称不能为空！");
					return;
				}
			  tSerShiftType.setName(Name.getText().getText().trim());
				try {
					tSerShiftTypeService.saveOrUpdateService(tSerShiftType);
					JOptionPane.showMessageDialog(null, "操作成功!");
					isSuccess=true;
					this.dispose();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					isSuccess=false;
				}				
			}else if(e.getSource() == cancel){
				this.dispose();
			}
		
	}
	private void initViews() {
		JPanel main_jp=new BorderJPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		number=new JLabelJTextFieldComponent("轮牌编号：",30);
		Name = new JLabelJTextFieldComponent("轮牌名称：", 30);
		number.getText().setText(UUID.GenUUID15());
		number.getText().setEditable(false);
		JPanel jp1=new JPanel();
		okButton=new RPJButtonBlue("添加");
		cancel = new RPJButtonGray("取消");
		
		jp1.add(okButton);
		jp1.add(cancel);
		jp1.setOpaque(false);
		
		main_jp.add(number);
		main_jp.add(Name);
		main_jp.add(jp1);
		main_jp.setBackground(new Color(247, 252, 255));
		main_jp.setCursor(Util.CURSOR_DEFAULT);
		this.add(main_jp);
	}
	public void showDialog(){
		this.setVisible(true);
	}
	public void showDialog(TSerShiftType tSerShiftType){
		if(tSerShiftType!=null){
			number.getText().setText(tSerShiftType.getTypeID());
			Name.getText().setText(tSerShiftType.getName());
		}
		this.setVisible(true);
	}
}
